package com.studentEasyui.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.studentEasyui.dao.StudentDao;
import com.studentEasyui.entity.StudentEntity;
@Repository
public class StudentDaoImpl implements StudentDao{
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	@Override
	public void addStudent(StudentEntity idNumber) {
		String sql = "insert into t_spring_easyui(name,id_number,age,candi_car,sex,phone,address,census_register) values (?,?,?,?,?,?,?,?)";
		getJdbcTemplate().update(sql, idNumber.getName(), idNumber.getIdNumber(), idNumber.getAge(), idNumber.getCandiCar(),idNumber.getSex(),idNumber.getPhone(),idNumber.getAddress(),idNumber.getCensusRegister());
		
	}

	@Override
	public int deleteStudent(Long id) {
		String sql = "update t_spring_easyui set delete_logo='Y' where id=?";
		return getJdbcTemplate().update(sql, id);
		
	}

	@Override
	public int updateStudent(StudentEntity idNumber) {
//		The method getPhone() is undefined for the type String  
//		getPhone()方法没有为类型字符串定义    因为定义是的studentEntity
		String sql = "update t_spring_easyui set name=?,id_number=?,age=?,candi_car=?,sex=?,phone=?,address=?,census_register=? where idNumber=?";
		return getJdbcTemplate().update(sql, idNumber.getName(), idNumber.getIdNumber(), idNumber.getAge(), idNumber.getCandiCar(),idNumber.getSex(),idNumber.getPhone(),idNumber.getAddress(),idNumber.getCensusRegister());
	
		
	}

	@Override
	public List<StudentEntity> listAll() {
		String sql = "select * from t_spring_easyui";
		return getJdbcTemplate().query(sql, new ShopRowMapper());
	}



	class ShopRowMapper implements RowMapper<StudentEntity> {
		// rs为返回结果集，以每行为单位封装着
		public StudentEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
			StudentEntity student = new StudentEntity();
			student.setId(rs.getLong("id"));
			student.setName(rs.getString("name"));
			student.setIdNumber(rs.getString("id_number"));
			student.setAge(rs.getString("age"));
			student.setSex(rs.getString("sex"));
			student.setPhone(rs.getString("phone"));
			student.setAddress(rs.getString("address"));
			student.setCensusRegister(rs.getString("census_register"));
			student.setCandiCar(rs.getString("candi_car"));
			student.setSchool(rs.getString("school"));
			student.setLearnSalary(rs.getString("learn_salary"));
			student.setApplyTime(rs.getTimestamp("apply_time"));
			student.setCreateName(rs.getString("create_name"));
			student.setDeleleLogo(rs.getString("delete_logo"));			
			return student;
		}
	}
	@Override
	public int login(String name, String idNumber) {
		String sql = "select * from t_spring_easyui where name = ? and id_number = ?"; 
		 Object[] params = new Object[2];
		 params[0] = name;
		 params[1] = idNumber;	  
	     return getJdbcTemplate().update(sql, params);
		
	}

	public StudentEntity getById(Long id) {
		String sql="select * from t_spring_easyui where id=?";
		try {
			// 如果没有查询到数据会报异常EmptyResultDataAccessException
			return getJdbcTemplate().queryForObject(sql, new Long[] { id }, new ShopRowMapper());
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public StudentEntity getById(StudentEntity id) {
		// TODO Auto-generated method stub
		return null;
	}

	

}
